package vn.allmobile.ambnews.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;

import com.aphidmobile.utils.IO;


import vn.allmobile.ambnews.data.Article;
import vn.allmobile.ambnews.data.CacheFile;
import vn.allmobile.ambnews.data.ListArticle;
public class Utils {
	
	public static String md5(String s) {
	    try {
	        // Create MD5 Hash
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();

	        // Create Hex String
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        return hexString.toString();

	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return "";
	}
	
	public static long currentMillis()
	{
		return System.currentTimeMillis();
	}
	
	public static String getCategoryImage(String ppid, String catid)
	{
		String fileName = Utils.md5(ppid + "_" + catid );
		
		String respone = CacheFile.readFile(GlobalApplication._(),fileName
			 , Contants.CACHE_ARTICLE_PATH);
		if(respone != null)
		{
			ListArticle  list = (ListArticle) IO.convertStringToObject(
					respone, ListArticle.class);			
			List<Article>  articles  = list.getListArticle();
			Article firstArticle  = articles.get(0);
			return firstArticle.getImage();
		}
		
		return null;
	}
	
	
	public static long datetime2timestamp(String datestring)
	{
		long dateinSecond = 0L;
		if(datestring != null)
		{
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sf.setTimeZone(TimeZone.getTimeZone("UTC"));
			ParsePosition pp = new ParsePosition(0); 
			Date date = sf.parse(datestring,pp);
			if (pp.getIndex() == 0) {
			    Log.v("Can't getDate with format: and value:\""+datestring + "\" at char index:"+pp.getErrorIndex());			    
			}
			dateinSecond  = date.getTime()/1000;
		}
		
		return dateinSecond;
	}
	
	public static String time_elapsed_string(String datestring) {
	    //$etime = time() - $ptime;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sf.setTimeZone(TimeZone.getTimeZone("UTC"));
		
		ParsePosition pp = new ParsePosition(0); 
		Date date = sf.parse(datestring,pp);
		if (pp.getIndex() == 0) {
		    Log.v("Can't getDate with format: and value:\""+datestring + "\" at char index:"+pp.getErrorIndex());
		    
		}
		//Calendar cal =  Calendar.getInstance();
		Calendar aUTCCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
	
		long currentSecond =  Utils.currentMillis() / 1000L; //cal.get(Calendar.SECOND);
		//cal.setTime(date);		
		long dateinSecond = (date.getTime()/1000L) - (7*60*60);//cal.get(Calendar.SECOND);				
		long etime = (currentSecond - dateinSecond );		
		
		//Log.v("currentSecond:" + currentSecond + "dateinSecond:" + dateinSecond);
		
	    if (etime < 1) {
	        return "0 seconds";
	    }
	    
	    Map<Long, String> arrayTime = new TreeMap<Long, String>(Collections.reverseOrder());
	    arrayTime.put((long) (1), "giây");
	    arrayTime.put((long) (60), "phút");
	    arrayTime.put((long) (60 * 60), "giờ");
	    arrayTime.put((long) (24 * 60 * 60), "ngày");
	    arrayTime.put((long) (30 * 24 * 60 * 60), "tháng");
	    arrayTime.put((long) (12 * 30 * 24 * 60 * 60), "năm");
	  
	    for (Map.Entry<Long,String> entry : arrayTime.entrySet()) {
	        Long key = entry.getKey();
	        String value = entry.getValue();
	        long tmpTime = etime/key;	        	       	        
	        if(tmpTime >= 1)
	        {
	        	long r = Math.round(tmpTime);
	        	return r + " " + value + (r > 1 ? " trước" : "");
	        }
	    }
		return "";
	    
	    
	}
	
	public static boolean isHoneyCombSupport()
	{
		if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
			  return true;
		}
		return false;
	}
}
